package fhv.itb.ec.auction;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Hashtable;
import java.util.Scanner;

import javax.naming.InitialContext;

public class AuctionClient extends UnicastRemoteObject implements ClientInterface {
	

	private static final long serialVersionUID = 1L;
	private static Auction srvAuction = null;
	
	protected AuctionClient() throws RemoteException, MalformedURLException, NotBoundException {
		super();
		//getting auction object from rmi registry
		srvAuction = (Auction) Naming.lookup("rmi://localhost/AuctionObject");
		//setting the client interface
		srvAuction.setClientInterface(this);
	}

	
	public static void main(String[] args) throws RemoteException {
		
		//getting codebase
		String codebase = "http://localhost:8080/Download/";
		System.setProperty("java.rmi.server.codebase", codebase);

		// create JNDI properties
		Hashtable props = new Hashtable();
		props.put("java.naming.factory.initial",
				"com.sun.jndi.rmi.registry.RegistryContextFactory");
		props.put("java.naming.provider.url", "rmi://localhost:1099");

		InitialContext initContext = null;

		try {
			//creating AuctionClient object and bind it in registry
			initContext = new InitialContext(props);
			AuctionClient client = new AuctionClient();
			initContext.rebind("ClientObject", client);
			System.out.println("AuctionClient bound in registry");
		} catch (Exception e) {
			System.out.println("AuctionImpl error");
			e.printStackTrace();
		}
		//Printing out instructions
		System.out.println("List of Items you can bid on:");
		System.out.println(srvAuction.getItemList());
		
		System.out.println("You can type in following commands:");
		
		System.out.println("join 'yourName''nameOfAuction'");
		System.out.println("bid 'yourName''nameOfAuction''yourActualBid'");
		System.out.println("leave 'yourName''nameOfAuction'");
		
		System.out.println("You can only bid on an auction you have joined");
		
		try {
			
			while (true) {
				//Obtaining command line from console
				Scanner in = new Scanner(System.in);
				String query = in.nextLine();
				query.toLowerCase();
				String[] commandLine = query.split(" ");
				
				//processing of join and leave command
				if (commandLine.length == 3){
					String command = commandLine[0];
					String bidderName = commandLine[1];
					String auctionName = commandLine[2];
				
					if (command.equals("join")) {
						System.out.println(srvAuction.joinAuction(bidderName, auctionName));
					} else if (command.equals("leave")) {
						System.out.println(srvAuction.leaveAuction(bidderName, auctionName));
					} else {
						System.out.println("you typed in a wrong command");
					}
				//processing of bid command
				} else if (commandLine.length == 4){
					String command = commandLine[0];
					String bidderName = commandLine[1];
					String auctionName = commandLine[2];
					String currentBid = commandLine[3];
					
					if (command.equals("bid")) {
						String bidStatus = srvAuction.bid(bidderName, auctionName, Integer.parseInt(currentBid));
						System.out.println(bidStatus);
						String success = bidStatus.substring(0, 7);
						//if placed successful bid, refresh clients
						if (success.equals("Success")) {	
							srvAuction.refresh();
						}
					} else {
						System.out.println("you typed in a wrong command");
					}
				} else {
					System.out.println("you typed in a wrong command line length");
				}
			}
			
			
		} catch (Exception e) {
			System.out.println("AuctionClient exception: " + e.getMessage());
			e.printStackTrace();
		}
	}

	//Method for refreshing client with actual bid information
	@Override
	public void printActualBidding(String item, int price)
			throws RemoteException {
		System.out.println("The current price of Item " + item + " is " + price);
		
	}



}
